package com.frontend.screens;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.data.documents.Invoice;
import com.frontend.DoctorsOfficeClient;

public class ViewInvoiceScreen extends Screen{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1751825810949617844L;
	private Screen prevScreen;
	private JLabel message, total, totalMessage;
	private JLabel[] items, prices;
	private JPanel billPanel, b1, b2, b3, b4;
	
	private JButton back;
	
	public ViewInvoiceScreen(DoctorsOfficeClient window, Invoice invoice, Screen prevScreen){
		super(window);
		this.prevScreen = prevScreen;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		b1 = new JPanel();
		b1.setOpaque(false);
		
		message = new JLabel("Invoice");
		message.setAlignmentX(CENTER_ALIGNMENT);
		
		b2 = new JPanel();
		b2.setOpaque(false);
		
		billPanel = new JPanel();
		items = new JLabel[invoice.getBill().size()];
		prices = new JLabel[invoice.getBill().size()];
		billPanel.setLayout(new GridLayout(items.length+1, 2));
		billPanel.setOpaque(false);
		
		List<String> bill = invoice.getBill();
		for(int i = 0; i< items.length; i++){
			items[i]= new JLabel(bill.get(i)+ ":  ");
			items[i].setHorizontalAlignment(SwingConstants.TRAILING);
			prices[i] = new JLabel("  $" + invoice.getItemCost(bill.get(i)) + ".00");
			prices[i].setHorizontalAlignment(SwingConstants.LEADING);
			billPanel.add(items[i]);
			billPanel.add(prices[i]);
		}
		
		totalMessage = new JLabel("Total:  ");
		totalMessage.setHorizontalAlignment(SwingConstants.TRAILING);
		total = new JLabel("  $" + invoice.calculateTotalCost() + ".00");
		total.setHorizontalAlignment(SwingConstants.LEADING);
		
		billPanel.add(totalMessage);
		billPanel.add(total);
		
		b3 = new JPanel();
		b3.setOpaque(false);
		
		back = new JButton("Back");
		back.addActionListener(new ViewInvoiceListener());
		back.setOpaque(false);
		back.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		b4 = new JPanel();
		b4.setOpaque(false);
		
		add(b1);
		add(message);
		add(b2);
		add(billPanel);
		add(b3);
		add(back);
		add(b4);
		
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			for(int i= 0; i< items.length; i++){
				items[i].setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 20));
				prices[i].setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 20));
				total.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 20));
				totalMessage.setFont(new Font("Arial", Font.PLAIN,
						getHeight() / 20));
			}
			
			back.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}
	
	private class ViewInvoiceListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource().equals(back)){
				window.changeScreen(prevScreen);
				prevScreen.repaint();
				if (prevScreen instanceof PatientOverviewScreen) {
					((PatientOverviewScreen) prevScreen).search();
				}
			}
		}
	}
}
